<html>
<head>
    <meta charset="UTF-8">
    <title>药库管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" >
</head>
<body>
<div class="demoTable" style="margin-left: 10px;margin-top: 10px">
    药品名:
    <div class="layui-inline">
        <input type="text" name="name" id="name" lay-verify="title" autocomplete="off"
               placeholder="请输入药物名称" class="layui-input">
    </div>
    剂型:
    <div class="layui-inline">
        <input type="text" name="drug_type" id="drug_type" lay-verify="title" autocomplete="off"
               placeholder="请输入剂型" class="layui-input">
    </div>
    是否限制药:
    <div class="layui-inline">
        <select id='limit_status' name="limit_status">
            <option value="">是否限制类药物</option>
            <option value="1">是</option>
            <option value="0">否</option>
        </select>
    </div>
    功效分类:
    <div class="layui-inline">
        <input type="text" name="efficacy_classification" id="efficacy_classification" lay-verify="title" autocomplete="off"
               placeholder="请输入药物功效" class="layui-input">
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<table class="layui-hide" id="demo" lay-filter="test"></table>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 自定义的控件 -->
<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    layui.use(['table', 'layer', 'jquery'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        //第一个实例
        table.render({
            elem: '#demo'
            , height: document.body.clientHeight - 180
            , url: '/drug/findAll' //数据接口
            , page: true
            , id: 'testReload'
            , cols: [[ //表头
                {field: 'id', title: '药品编号', sort: true}
                , {field: 'name', title: '药品名', sort: true}
                , {field: 'drug_type', title: '剂型', sort: true}
                , {field: 'specification', title: '规格', sort: true}
                , {field: 'unit', title: '规格单位', sort: true}
                , {field: 'limit_status', title: '是否限制药', sort: true,
                templet: function (d){
                    if (d.limit_status==0){
                        return '否';
                    }
                    if (d.limit_status==1){
                        return '是';
                    }
                }
                }
                , {field: 'efficacy_classification', title: '功效', sort: true}
                , {field: 'wholesale_price', title: '批发价', sort: true}
                , {field: 'price', title: '售价', sort: true}
                , {field: 'manufacturer', title: '生产厂家', sort: true}
                , {field: 'drug_count', title: '库存量', sort: true}
                , {field: 'production_date', title: '生产日期', sort: true,
                    templet: "<div>{{layui.util.toDateString(d.production_date, 'yyyy-MM-dd')}}</div>"
                }
                ,{field: 'quality_date', title: '保质日期', sort: true,
                    templet: "<div>{{layui.util.toDateString(d.quality_date, 'yyyy-MM-dd')}}</div>"
                },
                { title: '操作',width:120, toolbar: '#barDemo', align: 'center'}
            ]]
            , toolbar: '#toolbar',//表格头信息
            parseData: function (rs) {//数据格式解析
                console.log(rs);
                if (rs.code == 200) {
                    return {
                        "code": rs.code,
                        "msg": rs.msg,
                        "count": rs.data.total,
                        "data": rs.data.list
                    }
                }
            },
            response: {	// 设置响应码
                statusCode: 200 //规定成功的状态码，默认：0
            }
        });


        //监听工具条
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                open("用户修改页面","/view/drug/edit.html?id="+data.id);
            } else if (obj.event === 'del') {
                layer.confirm('要删除该药物吗？', function(index){
                    $.ajax({
                        type: "POST",
                        url: "/drug/del",
                        data: {id: data.id},
                        dataType:"json",
                        success: function (da) {
                            console.log(da)
                            if (da.code == 200) {
                                layer.msg(da.msg)
                                obj.del(); //删除对应行（tr）的DOM结构
                                // location.reload();//刷新当前页面
                            } else {
                                layer.msg(da.msg)
                            }
                        },
                        error: function (da) {
                            alert("出现错误");
                            return false;
                        }
                    }) //ajax结束
                    layer.close(index);
                });
            }
        });
        //"用户添加页面",
        function open(title,url){
            layer.open({
                type: 2
                ,title: title
                ,content:url
                ,offset: '50px'
                ,area: ['500px', '60%']
                ,end:function () {
                    table.reload('testReload');
                }
            });
        }

        function searchBtn(){

        }

        var $ = layui.$, active = {
            reload: function () {
                var name = $('#name');
                var drug_type = $('#drug_type');
                var limit_status = $('#limit_status');
                var efficacy_classification = $('#efficacy_classification');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        "name": name.val(),
                        "drug_type":drug_type.val(),
                        "limit_status":limit_status.val(),
                        "efficacy_classification":efficacy_classification.val(),
                    }
                });
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

</script>
</body>
</html>